package com.aqie.arithmetic.datastructure;

import java.util.Objects;

/**
 * 累加器
 */
public class Accumulator {
    protected double total;
    protected int N;
    public void addDataValue(double val){
        N++;
        total += val;
    }

    // 所有数据的平均值
    public double mean(){
        return total / N;
    }

    @Override
    public String toString(){
        return "Mean (" + N + " values): " + String.format("%7.5f", mean());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Accumulator that = (Accumulator) o;
        return Double.compare(that.total, total) == 0 &&
                N == that.N;
    }

    @Override
    public int hashCode() {
        return Objects.hash(total, N);
    }
}
